options mprint mlogic symbolgen spool ls=72 ps=54;

libname b_1999 "/Young_Surgeon/sas/Medicare/1999";
libname b_2000 "/Young_Surgeon/sas/Medicare/2000";
libname b_2001 "/Young_Surgeon/sas/Medicare/2001";
libname b_2002 "/Young_Surgeon/sas/Medicare/2002";
libname b_2003 "/Young_Surgeon/sas/Medicare/2003";
libname b_2009 "/Young_Surgeon/sas/Medicare/2009";
libname b_2010 "/Young_Surgeon/sas/Medicare/2010";
libname b_2011 "/Young_Surgeon/sas/Medicare/2011";
libname b_2012 "/Young_Surgeon/sas/Medicare/2012";
libname b_2013 "/Young_Surgeon/sas/Medicare/2013";
libname f "/Young_Surgeon/sas/sharpe/flags_linkage";
libname z "/Young_Surgeon/sas/zhou/data";
libname z2 "/Young_Surgeon/sas/zhou/test";
libname b "/Young_Surgeon/sas/sharpe/partb_linkage";
libname c "/Young_Surgeon/sas";


%let obsnum=max;
%let obsnumB=max;

***************************************************************************;
**** Read in the ICD 9-CPT crosswalk files. *******************************;


data icd9_cw;
set c.icd9_ppx_cats_bariatric_1222;
run;

data preferred_cpt_cw;
set c.preferred_cpt_codes_1222;
format CPT_Code_char $5.;
CPT_Code_char = input(CPT_Code, 12.);
run;

proc sort data=icd9_cw;
by Narrow_Procedure_Group;
run;

proc sort data=preferred_cpt_cw;
by Narrow_Procedure_Group;
run;



**************************************************************************;
**** Read in the AMA database. *******************************************;

proc sort data=z.surgeon_final_to_james out=ama_abms_upin (drop=NPI) nodupkey;
 by upin; run;
 
proc sort data=z.surgeon_final_to_james out=ama_abms_npi (drop=UPIN) nodupkey;
 by npi; run;

data ama_abms_upin;
 set ama_abms_upin;
 where upin ne "";
 run;

data ama_abms_npi;
 set ama_abms_npi;
 where npi ne "";
 run;
 
 ****************************************************************************; 
**** Read in the ICD 9 file. ***********************************************;

proc import datafile = "/Young_Surgeon/sas/sharpe/datachecks/YS_ICD9_Procedure_Code_List_20160114_FINAL.xlsx"
	out=icd9list
	dbms=xlsx;
run;

**** Extract only the code and label. ****;

data icd9list_only;
set icd9list;
keep ICD9_Code ICD9_label;
rename ICD9_Code=ICD_PRCDR_CD1;
run;

proc sort data=icd9list_only;
by ICD_PRCDR_CD1;
run;



%macro loop_era_surg(dat=, surg=, date=);


****************************************;
**** Read in the inpatient records. ****;

%let liver_procs = ('4563' '5059');

data ip_&dat._&surg.;
set f.pre_linkage_data;
if &surg._adm_&dat.=1 and (ICD_PRCDR_CD1 not in &liver_procs.);
run;


***********************************;
**** Read in the part B files. ****;

%let keepl=BENE_ID CLM_THRU_DT CLM_ID PRF_PHYSN_UPIN PRF_PHYSN_NPI PRVDR_SPCLTY 
 LINE_PLACE_OF_SRVC_CD HCPCS_CD HCPCS_1ST_MDFR_CD HCPCS_2ND_MDFR_CD 
 LINE_SBMTD_CHRG_AMT CARR_LINE_MTUS_CNT CARR_LINE_MTUS_CD LINE_NCH_PMT_AMT;


%let hcpcs_first = ('A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N'
			'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'
			'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n'
			'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' 
			'0' '8' '9');


%if &dat.=trad %then %do;

	data all_partb_line_&dat.;
	set b_1999.bcarrier_line_j_1999: (KEEP=&keepl) 
		b_2000.bcarrier_line_j_2000: (KEEP=&keepl) 
		b_2001.bcarrier_line_j_2001: (KEEP=&keepl)
		b_2002.bcarrier_line_j_2002: (KEEP=&keepl)
		b_2003.bcarrier_line_j_2003: (KEEP=&keepl);
	rename CLM_THRU_DT=CLM_THRU_DT_line PRF_PHYSN_UPIN=UPIN PRF_PHYSN_NPI=NPI;
	hcpcs_first_letter = substr(HCPCS_CD,1,1);
	if hcpcs_first_letter not in &hcpcs_first.;
	run;
	
%end;

%if &dat.=mod %then %do;
	
	data all_partb_line_&dat.;
 	set b_2009.bcarrier_line_j_2009: (KEEP=&keepl) 
 		 b_2010.bcarrier_line_j_2010: (KEEP=&keepl) 
		 b_2011.bcarrier_line_j_2011: (KEEP=&keepl)
		 b_2012.bcarrier_line_j_2012: (KEEP=&keepl) 
		 b_2013.bcarrier_line_j_2013: (KEEP=&keepl); 
	rename CLM_THRU_DT=CLM_THRU_DT_line PRF_PHYSN_UPIN=UPIN PRF_PHYSN_NPI=NPI;
	hcpcs_first_letter = substr(HCPCS_CD,1,1);
	if hcpcs_first_letter not in &hcpcs_first.;
	run;

%end;



 **********************************************************;
 **** Merge the inpatient file to the ICD-9 crosswalk. ****;
 
  proc sql;
  create table merged_ip_&dat._&surg. as
  select a.*,b.*
  from ip_&dat._&surg. as a left join icd9_cw as b
  on a.ICD_PRCDR_CD1=b.ICD9_Text;
  quit; run;
  
  proc datasets lib=work nolist nodetails;
  delete ip_&dat._&surg.;
  run;

  
  ***********************************************;
  **** Separate out the bariatric patients. *****;
  **** Change procedure groups if necessary. ****;
  **** Then, recombine with rest of data. *******;
  
  %if &surg.=gensurg %then %do;
  
	 %let bar_proc = ("437" "4382" "4389" "443" "4431" "4438" "4439" "4468"
		"4469" "4495" "4551" "4562" "459" "4590" "4591");
 	 %let ob_surg_drg_trad = ("288");
 	 %let ob_surg_drg_mod = ("619" "620" "621");
 	 %let ob_dx_code = ("278" "2780" "27800" "27801" "2781" "V853" "V8530" "V8531"
 			"V8532" "V8533" "V8534" "V8535" "V8536" "V8537" "V8538"
 			"V8539" "V8540");
 			
	 data merged_ip_&dat._&surg.;
	 set merged_ip_&dat._&surg.;
	 array icd_array {*} ICD_PRCDR_CD1 - ICD_PRCDR_CD25;
	 true_bariatric=0;
 
	       %if &dat.=trad %then %do;
		 	do i=1 to dim(icd_array) until (true_bariatric=1);
	 		if (CLM_DRG_CD in &ob_surg_drg_trad.) or
	 			(icd_array{i} in &ob_dx_code.) then true_bariatric=1;
	 	
	 		end;
 		%end;
 	
 		%if &dat.=mod %then %do;
 			do i=1 to dim(icd_array) until (true_bariatric=1);
 				if (CLM_DRG_CD in &ob_surg_drg_mod.) or
 					(icd_array{i} in &ob_dx_code.) then true_bariatric=1;
 			end;
 		%end;
 
 	if (ICD_PRCDR_CD1 in ('4382' '4431' '4495') and true_bariatric=0) then DELETE;
 	if true_bariatric=0 and ICD_PRCDR_CD1 in ('4389') then
 		Narrow_Procedure_Group='stomach partial gastrectomy';
	 if true_bariatric=0 and ICD_PRCDR_CD1 in ('443' '4438' '4439') then
	 	Narrow_Procedure_Group='stomach gastric bypass (non-bariatric)';
	 if true_bariatric=0 and ICD_PRCDR_CD1 in ('4468' '4469') then
	 	Narrow_Procedure_Group='stomach other';
	 if true_bariatric=0 and ICD_PRCDR_CD1 in ('4551' '4562') then
	 	Narrow_Procedure_Group='small bowel resection';
	 run;

 	
 
 %end;

 
 
 
  
  
 ***********************************************************;
 **** Merge the part B line files to the CPT crosswalk. ****;
 

  proc sql;
  create table merged_pb_&dat._&surg. as
  select a.*,b.*
  from all_partb_line_&dat as a left join preferred_cpt_cw as b
  on a.HCPCS_CD=b.CPT_Code_char;
  quit; run;
  
  proc datasets lib=work nolist nodetails;
  delete all_partb_line_&dat.;
  run;
  
  
  
  
  *******************************************************************;
  **** Merge all inpatient to Part B. *******************************;
 
  PROC SQL;
  CREATE TABLE merged_wcw_&dat._&surg. as
  SELECT a.*,b.*,
  	(a.Narrow_Procedure_Group=b.Narrow_Procedure_Group) as icdcpt_group_flag,
  	a.Narrow_Procedure_Group as ip_npg,
  	b.Narrow_Procedure_Group as pb_npg,
  	(a.Broad_Category=b.Broad_Category) as broad_group_flag,
  	a.Broad_Category as ip_bpg,
  	b.Broad_Category as pb_bpg,
  	(a.PRCDR_DT1-b.CLM_THRU_DT_line=0) as same_date_flag,
  	(b.HCPCS_CD^=" " and (b.CLM_THRU_DT_line-a.PRCDR_DT1=0)) as HCPCS_same_date_flag,
  	(b.BENE_ID is null) as merge_pb,
  	(a.PRCDR_DT1-b.CLM_THRU_DT_line) as PRCDR_DT_minus_CLM,
  	(case when (a.PRCDR_DT1-b.CLM_THRU_DT_line)<0 then 1 else 0 end) as neg_date,
  	abs(a.PRCDR_DT1-b.CLM_THRU_DT_line) as abs_date,
  	(a.Narrow_Procedure_Group^=b.Narrow_Procedure_Group and a.Broad_Category=b.Broad_Category) as pat_reassigned
  	
  FROM merged_ip_&dat._&surg. as a LEFT JOIN merged_pb_&dat._&surg. as b
  ON a.BENE_ID=b.BENE_ID and a.Broad_Category=b.Broad_Category
  WHERE a.clm_admsn_dt<=b.clm_thru_dt_line<=a.nch_bene_dschrg_dt
  ORDER a.BENE_ID, a.CLM_ID, icdcpt_group_flag descending, broad_group_flag descending, same_date_flag descending,
  	neg_date descending, abs_date descending, b.LINE_NCH_PMT_AMT descending;
  QUIT; RUN;
  
  proc datasets lib=work nolist nodetails;
  delete merged_ip_&dat._&surg. merged_pb_&dat._&surg.;
  run;
  
  **** Count how many qualifying CPT bills each admission had. ****;
  
  data merged_wcw_&dat._&surg.;
  set merged_wcw_&dat._&surg.;
  count=1;
  run;
  
  proc means data=merged_wcw_&dat._&surg. noprint;
  by BENE_ID CLM_ID;
  var count;
  output out=how_many_qcpts sum=sum_count;
  run;
  
  ods pdf file="/Young_Surgeon/sas/sharpe/partb_linkage/how_many_qcpts_&dat._&surg._&date..pdf";
  
  proc univariate data=how_many_qcpts;
  var sum_count;
  run;
  
  ods pdf close;
  
  **** Get distribution of date difference before selecting. ****;
  
  ods pdf file="/Young_Surgeon/sas/sharpe/partb_linkage/date_diff_all_&dat._&surg._&date..pdf";
  
  proc univariate data=merged_wcw_&dat._&surg.;
  var PRCDR_DT_minus_CLM;
  run;
  
  data merged_diff_not_zero;
  set merged_wcw_&dat._&surg.;
  if PRCDR_DT_minus_CLM ^= 0;
  run;
  
  proc univariate data=merged_diff_not_zero;
  var PRCDR_DT_minus_CLM;
  run;
  
  ods pdf close;
  
  proc sort data=merged_wcw_&dat._&surg. nodupkey;
  by BENE_ID CLM_ID;
  run;
  
  **** Get distribution of CPT date minus PPX date. ****;
  
  ods pdf file="/Young_Surgeon/sas/sharpe/partb_linkage/dist_date_diff_&dat._&surg._&date..pdf";
  
  proc univariate data=merged_wcw_&dat._&surg.;
  var PRCDR_DT_minus_CLM;
  run;
  
  data diff_date_not_zero;
  set merged_wcw_&dat._&surg.;
  if PRCDR_DT_minus_CLM^=0;
  run;
  
  proc univariate data=diff_date_not_zero;
  var PRCDR_DT_minus_CLM;
  run;
  
  ods pdf close;
  

proc datasets lib=work nolist nodetails;
delete diff_date_not_zero merged_diff_not_zero how_many_qcpts;
run;


******************************************************************;
**** Merge the ip-partb-merged files to the AMA surgeon data. ****;


%macro merge_upin_npi(id=);

proc sort data=merged_wcw_&dat._&surg.;
by &id.;
run;

data merged_wcw_&dat._&surg.;
merge merged_wcw_&dat._&surg. (in=a) ama_abms_&id. (in=b);
by &id.;
if a;
merge_ama_by_&id. = 0;
if (a=1 and b=1) then merge_ama_by_&id.=1;
run;


**** Create variables for qualifying bills. ****;

data merged_wcw_&dat._&surg.;
set merged_wcw_&dat._&surg.;
qcpt_same_date_flag=0;
if (HCPCS_same_date_flag=1 and icdcpt_group_flag=1) then qcpt_same_date_flag=1;
qcpt_ama_&id._flag=0;
if (merge_ama_by_&id.=1 and icdcpt_group_flag=1) then qcpt_ama_&id._flag=1;
qual_date_and_&id.=0;
if (merge_ama_by_&id.=1 and HCPCS_same_date_flag=1 and icdcpt_group_flag=1)
	then qual_date_and_&id.=1;
any_cpt_linked_same_date=0;
if (merge_ama_by_&id.=1 and HCPCS_same_date_flag=1) then any_cpt_linked_same_date=1;
run;


 **** Get frequency of non-qualifying CPTs. ****;
  
data no_qual_cpt_&dat._&surg.;
set merged_wcw_&dat._&surg.;
if icdcpt_group_flag=0;
run;
  
proc freq data=no_qual_cpt_&dat._&surg.;
table ICD_PRCDR_CD1*HCPCS_CD / out=no_qual_cpt;
run;
  
proc export data=no_qual_cpt
	outfile="/Young_Surgeon/sas/sharpe/partb_linkage/no_qual_cpt_&date._&dat._&surg..csv"
	dbms=csv replace;
run;

proc datasets lib=work nolist nodetails;
delete no_qual_cpt no_qual_cpt_&dat._&surg.;
run;


**** Check the merge success rate. ****;

title "AMA &id. merge success rate";
proc freq data=merged_wcw_&dat._&surg.;
tables merge_ama_by_&id. / out=ama_&id._success_&dat._&surg.;
where merge_pb=0 and &id. ne "";
run;
title "";

proc export data=ama_&id._success_&dat._&surg.
	outfile="/Young_Surgeon/sas/sharpe/partb_linkage/ama_&id._success_&dat._&surg._&date..csv"
	dbms=csv replace;
run;

proc datasets lib=work nolist nodetails;
delete ama_&id._success_&dat._&surg.;
run;

%mend merge_upin_npi;

%merge_upin_npi(id=UPIN);
%merge_upin_npi(id=NPI);


**** Create a table of admissions with qualifying CPTs. ****;

proc sort data=merged_wcw_&dat._&surg.;
by ICD_PRCDR_CD1 Narrow_Procedure_Group;
run;

proc means data=merged_wcw_&dat._&surg. noprint;
by ICD_PRCDR_CD1 Narrow_Procedure_Group;
var HCPCS_same_date_flag qcpt_same_date_flag icdcpt_group_flag
	merge_ama_by_upin qcpt_ama_upin_flag merge_ama_by_npi
	qcpt_ama_npi_flag qual_date_and_upin qual_date_and_npi
	any_cpt_linked_same_date;
output out=means_wcw_&dat._&surg.
	sum= / autoname;
run;

data means_wcw_&dat._&surg.;
set means_wcw_&dat._&surg.;
ICD_PRCDR_CD1 = '_' || strip(ICD_PRCDR_CD1);
run;


proc export data=means_wcw_&dat._&surg.
	outfile="/Young_Surgeon/sas/sharpe/partb_linkage/means_wcw_&dat._&surg._&date..csv"
	dbms=csv replace;
run;



**** Create a table showing whether each patient was reassigned. ****;

proc sort data=merged_wcw_&dat._&surg.;
%if &dat.=trad %then %do;
by ip_npg pb_npg ip_bpg pb_bpg merge_ama_by_UPIN;
%end;
%if &dat.=mod %then %do;
by ip_npg pb_npg ip_bpg pb_bpg merge_ama_by_NPI;
%end;
run;

data merged_wcw_&dat._&surg.;
set merged_wcw_&dat._&surg.;
count=1;
run;

proc means data=merged_wcw_&dat._&surg. noprint;
%if &dat.=trad %then %do;
by ip_npg pb_npg ip_bpg pb_bpg merge_ama_by_UPIN;
%end;
%if &dat.=mod %then %do;
by ip_npg pb_npg ip_bpg pb_bpg merge_ama_by_NPI;
%end;
var count;
output out=pat_reassign_&dat._&surg. sum=sum_count;
run;

proc export data=pat_reassign_&dat._&surg.
	outfile="/Young_Surgeon/sas/sharpe/partb_linkage/pat_reassign_&dat._&surg..csv"
	dbms=csv replace;
run;

**** Create summary files for pat_reassingn files. ****;

**** Get counts of each narrow procedure group. ****;

proc means data=pat_reassign_&dat._&surg. noprint;
var sum_count;
by ip_npg;
output out=overall_sum sum=N_admissions;
run;

data overall_sum;
set overall_sum (keep=ip_npg N_admissions);
run;

**** Get counts of how many of each narrow procedure groups were not reassigned. ****;

data not_reassigned;
set pat_reassign_&dat._&surg.;
if ip_npg=pb_npg;
run;

proc means data=not_reassigned noprint;
var sum_count;
by ip_npg;
output out=not_reassigned_sum sum=N_w_same_NPG;
run;

data not_reassigned_sum;
set not_reassigned_sum (keep=ip_npg N_w_same_NPG);
run;


**** Get counts of those reassigned. ****;

data reassigned;
set pat_reassign_&dat._&surg.;
if ip_npg^=pb_npg;
run;

proc means data=reassigned noprint;
var sum_count;
by ip_npg;
output out=reassigned_sum sum=N_wo_same_NPG;
run;

data reassigned_sum;
set reassigned_sum(keep=ip_npg N_wo_same_NPG);
run;

**** Merge all of the files together. ****;

data all_together;
merge overall_sum (in=a) not_reassigned_sum (in=b) reassigned_sum (in=c);
by ip_npg;
run;

**** Create percent variables. ****;

data all_together;
set all_together;
percent_w_same_NPG = 100*(N_w_same_NPG/N_admissions);
percent_wo_same_NPG = 100*(N_wo_same_NPG/N_admissions);
run;



**** Create a version of the orginial not broken down by merge status. ****;

proc means data=pat_reassign_&dat._&surg. noprint;
var sum_count;
by ip_npg pb_npg ip_bpg pb_bpg;
output out=reassign_v2_&dat._&surg. sum=sum_count;
run;

**** Export datafiles. ****;

proc export data=reassign_v2_&dat._&surg.
	outfile="/Young_Surgeon/sas/sharpe/partb_linkage/reassign_v2_&dat._&surg..csv"
	dbms=csv replace;
run;

proc export data=all_together
	outfile="/Young_Surgeon/sas/sharpe/partb_linkage/reassign_summary_&dat._&surg..csv"
	dbms=csv replace;
run;



**** If a broad category was used with a different NPG, change the patients NPG to the NPG of the CPT bill. ****;
data merged_wcw_&dat._&surg.;
set merged_wcw_&dat._&surg.;
format surgeon_type $4.;
surgeon_type="None";
if Trad_New_Surgeon_flag_final=1 then surgeon_type="New";
else if Mod_New_Surgeon_flag_final=1 then surgeon_type="New";
else if Trad_Exp_Surgeon_flag_final=1 then surgeon_type="Exp";
else if Mod_Exp_Surgeon_flag_final=1 then surgeon_type="Exp";

if pat_reassigned=1 then ip_npg=pb_npg;
run;

ods pdf file="/Young_Surgeon/sas/sharpe/partb_linkage/freq_npg_surg_&dat._&surg._&date..pdf";

proc freq data=merged_wcw_&dat._&surg.;
tables ip_npg*surgeon_type / out=freq_npg_surg;
run;

ods pdf close;

**** Save the part-B-linked data. ****;

data b.merged_wcw_&dat._&surg.;
set merged_wcw_&dat._&surg.;
run;

proc datasets lib=work nolist nodetails;
delete merged_wcw_&dat._&surg.
	means_wcw_&dat._&surg.
	overall_sum
	pat_reassign_&dat._&surg.
	not_reassigned
	reassigned
	not_reassigned_sum
	reassigned_sum
	all_together
	reassign_v2_&dat._&surg.
	freq_npg_surg;
run;


%mend loop_era_surg;

%loop_era_surg(dat=trad, surg=ortho, date=2_9);
%loop_era_surg(dat=trad, surg=gensurg, date=2_9);
%loop_era_surg(dat=mod, surg=ortho, date=2_9);
%loop_era_surg(dat=mod, surg=gensurg, date=2_9);
